package com.example.service;

import com.example.entity.Im;
import com.example.entity.User;
import com.example.mapper.ImMapper;
import com.example.utils.TimeUtils;
import com.example.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ImService {

    @Resource
    UserService userService;
    @Resource
    ImMapper imMapper;
    public void save(Im im) {
        User user=userService.selectByUid(im.getUid());
        im.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        im.setUid(user.getUid());
        im.setAvatar(user.getAvatar());
        im.setUserName(user.getName());
        im.setDelTag("0");
        imMapper.insert(im);
    }

    public List<Im> selectAll(Im im) {
        List<Im> ims=imMapper.selectAll(im);
        ims.forEach(entity->entity.setReleaseTime(TimeUtils.TimestampToDateFormat(entity.getCreateTime())));
        return ims;
    }
    public void deleteById(Integer id){
        imMapper.deleteById(id);
    }

    public List<Im> selectAllInit(Integer limit) {
        List<Im> ims=imMapper.selectAllInit(limit);
        ims.forEach(entity->entity.setReleaseTime(TimeUtils.TimestampToDateFormat(entity.getCreateTime())));
        return ims.stream().sorted(Comparator.comparing(Im::getId)).collect(Collectors.toList());
    }
}
